package com.ccb.jyzx.expand.tiantouonline.controller;

import com.ccb.jyzx.expand.tiantouonline.bean.Dict;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.DictBo;
import com.ccb.jyzx.expand.tiantouonline.bean.bo.PageBo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.DictVo;
import com.ccb.jyzx.expand.tiantouonline.bean.vo.PageBean;
import com.ccb.jyzx.expand.tiantouonline.service.DictService;
import com.ccb.jyzx.expand.tiantouonline.service.PageService;
import com.ccb.jyzx.expand.tiantouonline.view.response.SimpleResponseResult;
import com.ccb.jyzx.expand.tiantouonline.view.result.Result;
import com.ccb.jyzx.expand.tiantouonline.view.result.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: extend
 * @Author: jianghaolin
 * @Version: 1.0
 */
@Controller
@RequestMapping("/tto/dict")
@Api(tags = "分类项操作相关接口")
public class DictController extends BaseController<Dict> {

    @Autowired
    private DictService dictService;
    @Autowired
    private PageService pageService;

    @ResponseBody
    @PostMapping(value = "findAll",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "查询所有分类项", notes = "查询所有分类项")
    private SimpleResponseResult<List<Dict>> findAll(@RequestBody DictBo bo){
        List<Dict> dicts = dictService.findAll(bo);
        return new SimpleResponseResult<List<Dict>>(true,0, dicts,"查询成功");
    }

    @ResponseBody
    @PostMapping(value = "getPage",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "分页查询所有分类项", notes = "分页查询所有分类项")
    private Result getPage(@RequestBody PageBo bo){
        if(bo == null){
            return ResultUtil.error("缺少参数");
        }
        Integer currPage = bo.getCurrPage();
        Integer pageSize = bo.getPageSize();
        int totalCount = dictService.findTotleCount(bo);
        List<DictVo> vos = new ArrayList<>();
        if(totalCount > 0){
            vos = dictService.findByPage(bo);
        }
        PageBean pageBean = pageService.getPage(currPage, pageSize, totalCount, vos);
        return ResultUtil.success(pageBean);
    }

    @ResponseBody
    @PostMapping(value = "insert",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "保存分类项", notes = "保存分类项")
    private SimpleResponseResult<String> insert(@RequestBody DictBo pojo){
        try {
            dictService.save(pojo);
            return new SimpleResponseResult<String>(true,0, "保存成功","请求成功");
        }catch (SQLException e){
            return new SimpleResponseResult<String>(false,500, "保存失败","请求成功");
        }

    }

    @ResponseBody
    @PostMapping(value = "update",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "修改分类项", notes = "修改分类项")
    private SimpleResponseResult<String> update(@RequestBody DictBo pojo){
        try {
            dictService.update(pojo);
            return new SimpleResponseResult<String>(true,0, "修改成功","请求成功");
        }catch (SQLException e){
            return new SimpleResponseResult<String>(false,500, "修改失败","请求成功");
        }

    }

    @ResponseBody
    @PostMapping(value = "deleteMore",consumes = "application/json;charset=UTF-8")
    @ApiOperation(value = "删除分类项", notes = "删除分类项")
    private Result deleteMore(@RequestBody DictBo pojo){
        if(pojo == null){
            return ResultUtil.error("未接收到参数");
        }
        String ids = pojo.getIds();
        try {
            dictService.deleteMore(ids);
            return ResultUtil.success("删除成功");
        }catch (SQLException e){
            return ResultUtil.error(e.getMessage());
        }
    }
}
